<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <link rel="stylesheet" href="/layui/css/layui.css" media="all">
</head>
<body>
<form class="layui-form" lay-filter="address">
  <div id="id" class="layui-form-item">
    <label class="layui-form-label">id</label>
    <div class="layui-input-inline">
      <input readonly type="text" name="id" autocomplete="off" class="layui-input">
    </div>
  </div>


  <div class="layui-form-item">
    <label class="layui-form-label">省</label>
    <div class="layui-input-inline">
      <select id="province" name="provinceId" required lay-verify="required" lay-filter="province">
        <option value="">请选择</option>
      </select>
    </div>
  </div>


  <div class="layui-form-item">
    <label class="layui-form-label">市</label>
    <div class="layui-input-inline">
      <select id="city" name="cityId" required lay-verify="required" lay-filter="city">
        <option value="">请选择</option>
      </select>
    </div>
  </div>

  <div class="layui-form-item">
    <label class="layui-form-label">区</label>
    <div class="layui-input-inline">
      <select id="county" name="countyId" required lay-verify="required" lay-filter="county">
        <option value="">请选择</option>
      </select>
    </div>
  </div>

  <div class="layui-form-item">
    <label class="layui-form-label">详细信息</label>
    <div class="layui-input-inline">
      <textarea  name="detail" placeholder="输入详细信息" class="layui-textarea"></textarea>
    </div>
  </div>

  <div class="layui-form-item layui-form-text">
    <label class="layui-form-label">备注</label>
    <div class="layui-input-block">
      <textarea name="remark" placeholder="请输入" class="layui-textarea"></textarea>
    </div>
  </div>

  <div class="layui-form-item">
    <div class="layui-input-block">
      <button id="insert" class="layui-btn" lay-submit lay-filter="insert">立即提交</button>
      <button id="update" class="layui-btn" lay-submit lay-filter="update">立即提交</button>
      <button id="reset" type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
</form>
<script src="/layui/layui.js" charset="utf-8"></script>
<script src="/jquery/jquery-3.5.1.min.js"></script>
<script>

  // 先初始化
  layui.use(['form', 'layer'], function () {
    const form = layui.form;
    const layer = layui.layer;
    $.ajaxSettings.async = false;
    $.get("/api/area", function (data){
      updateOption('province', data.data);
      form.render('select');
      console.log("省加载完成")
    });
    $.ajaxSettings.async = true;

    // 加载市
    form.on('select(province)', function(data){
      $.get("/api/area?id="+data.value, function (data){
        updateOption('city', data.data)
        form.render('select');
        console.log("市加载完成")
      });
    });

    // 加载区县的
    form.on('select(city)', function(data){
      console.log(data.value); //得到被选中的值
      $.get("/api/area?id="+data.value, function (data){
        updateOption('county', data.data)
        form.render('select');
        console.log("区加载完成")
      });
    });

    // 新增
    form.on('submit(insert)', function (data) {
      const params = JSON.stringify(data.field);
      console.log(params);
      $.ajax({
        url: "/api/address",
        method: 'post',
        contentType: 'application/json',
        data: params,
        dataType: 'json',
        success: function (data) {
          if (data.code === 200) {
            layer.msg('添加成功')
            // 1秒后刷新
            setTimeout(function () {
              window.parent.location.reload();
            }, 1500);
          } else if (data.code === 100) {
            layer.msg(data.message);
          }
        }
      });
      return false;
    });
    // 修改
    form.on('submit(update)', function (data) {
      const params = JSON.stringify(data.field);
      const id = data.field.id;
      $.ajax({
        url: "/api/address/" + id,
        method: 'put',
        contentType: 'application/json',
        data: params,
        dataType: 'json',
        success: function (data) {
          if (data.code === 200) {
            layer.msg('修改成功')
            setTimeout(function () {
              window.parent.location.reload();
            }, 1500);
          } else if (data.code === 100) {
            layer.msg(data.message);
          }
        }
      });
      return false;
    })
    console.log("渲染完成")
  });

  // 最后赋值
  function formData(data) {
    console.log("开始赋值")
    layui.use(['form', 'layer'], function () {
      const form = layui.form;
      const provinceId = data.provinceId;
      const cityId = data.cityId;
      $.ajaxSettings.async = false;
      $.get("/api/area?id="+provinceId, function (data){
        updateOption('city', data.data)
        form.render('select');
        console.log("市加载完成")
      });
      $.get("/api/area?id="+cityId, function (data){
        updateOption('county', data.data)
        form.render('select');
        console.log("区县加载完成")
      });
      $.ajaxSettings.async = true;
      form.val("address", JSON.parse(JSON.stringify(data)));
    });
  }

  function updateOption(id, data){
    $('#'+id +" option").remove();
    $('#'+id).append("<option value=''>请选择</option>")
    for (let i = 0; i < data.length; i++) {
      const item = data[i];
      $('#'+id).append("<option value='" + item.id + "'>" + item.areaname + "</option>")
    }
    if (id === 'province'){
      $("#county option").remove();
      $("#count").append("<option value=''>请选择</option>");
    }
  }
</script>
</body>
</html>